/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DAO.util.ConectaBanco;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.ClassificacaoGeral;
import model.Time;

/**
 *
 * @author Alex
 */
public class ClassificacaoGeralDAO {

    public boolean atualizar(ClassificacaoGeral classificacaoGeral) {
        try {

            Connection conexao = ConectaBanco.getConexao();
            String sql = "update classificacao_geral set id_time=? where id=?";
            PreparedStatement pstmt = conexao.prepareCall(sql);
            System.out.println("O time na classificação geral é:"+classificacaoGeral.getTime().getNome_time() );
            pstmt.setInt(1, classificacaoGeral.getTime().getId_time());
            pstmt.setInt(2, classificacaoGeral.getId());

            pstmt.execute();
            conexao.close();

            return true;
        } catch (SQLException sql) {
            return false;

        }
    }

    public ArrayList<ClassificacaoGeral> abastecerChave(int chave1, int chave2) {
        ArrayList<ClassificacaoGeral> classificacoes = new ArrayList<ClassificacaoGeral>();
        Connection conexao = null;
        try {
            conexao = ConectaBanco.getConexao();
            String sql = "select * from classificacao_geral where id_chave between ? and ? order by id ";

            PreparedStatement pst = conexao.prepareStatement(sql);

            pst.setInt(1, chave1);
            pst.setInt(2, chave2);
            ResultSet rs = pst.executeQuery();
            TimeDAO timeDAO = new TimeDAO();
            ChaveDAO chaveDAO = new ChaveDAO();

            while (rs.next()) {
                ClassificacaoGeral classificacaoGeral = new ClassificacaoGeral();
                classificacaoGeral.setId(rs.getInt("id"));
                classificacaoGeral.setChave(chaveDAO.buscarPorId(rs.getInt("id_chave")));
                classificacaoGeral.setTime(timeDAO.buscarPorId(rs.getInt("id_time")));
                classificacaoGeral.setPosicao(rs.getString("posicao"));

                classificacoes.add(classificacaoGeral);
            }

            conexao.close();
            return classificacoes;

        } catch (Exception sql) {
            System.out.println("Erro ao buscar um aluno" + sql.getMessage());
            return null;
        }

    }

}
